package org.fioma.ui;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import org.fioma.general.Read;
import org.fioma.general.ReadStrings;

public class ExtractFastaFromLog {

	public static long case1 = 0, case2 = 0, case3 = 0;

	public static void main(String[] args) {

		try {
			long timeInic = System.currentTimeMillis();
			String matePairsFileName = args[0];
			String logExtension = args[1];

			System.out.print("Arguments:");
			for (String arg : args) {
				System.out.print(" " + arg);
			}
			System.out.println();

			matePairsHandler(matePairsFileName, logExtension);

			System.out.println("case1:" + case1);
			System.out.println("case2:" + case2);
			System.out.println("case3:" + case3);
			System.out.println();
			System.out.println("Time: " + (System.currentTimeMillis() - timeInic));
			System.out.println();

		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
		}
	}

	public static void matePairsHandler(String fileName, String extension) throws Exception {
		BufferedReader matePairsReads = new BufferedReader(new FileReader(fileName));

		String line;

		String file1, file2;
		String[] files;
		String line1File1;
		String line2File1;
		String line3File1;
		String line4File1;
		String line1File2;
		String line2File2;
		String line3File2;
		String line4File2;

		String lineLog;
		String[] logs;

		Read read1, read2;

		boolean gaps1, gaps1a, gaps2, gaps2a;
		int junctions1, junctions1a, junctions2, junctions2a;

		while ((line = matePairsReads.readLine()) != null) {
			files = line.split(";");
			file1 = files[0];
			file2 = files[1];
			System.out.println("Starting the mate pair files:" + line);

			BufferedReader matePairsFile1 = new BufferedReader(new FileReader(file1));
			BufferedReader matePairsFile2 = new BufferedReader(new FileReader(file2));
			BufferedReader matePairsFileLog = new BufferedReader(new FileReader(file1 + extension));
			matePairsFileLog.readLine(); // read the header

			BufferedWriter matePairsFile1Gaps = new BufferedWriter(new FileWriter(file1 + extension + ".gaps.fasta", false));
			BufferedWriter matePairsFile2Gaps = new BufferedWriter(new FileWriter(file2 + extension + ".gaps.fasta", false));
			BufferedWriter matePairsFile1Junction = new BufferedWriter(new FileWriter(file1 + extension + ".junction.fasta", false));
			BufferedWriter matePairsFile2Junction = new BufferedWriter(new FileWriter(file2 + extension + ".junction.fasta", false));
			BufferedWriter matePairsFile1Match = new BufferedWriter(new FileWriter(file1 + extension + ".match.fasta", false));
			BufferedWriter matePairsFile2Match = new BufferedWriter(new FileWriter(file2 + extension + ".match.fasta", false));

			while ((lineLog = matePairsFileLog.readLine()) != null) {
				line1File1 = matePairsFile1.readLine();
				line2File1 = matePairsFile1.readLine();
				line3File1 = matePairsFile1.readLine();
				line4File1 = matePairsFile1.readLine();

				line1File2 = matePairsFile2.readLine();
				line2File2 = matePairsFile2.readLine();
				line3File2 = matePairsFile2.readLine();
				line4File2 = matePairsFile2.readLine();

				logs = lineLog.split("\t");

				read1 = new ReadStrings(line1File1, line2File1, line4File1);
				read2 = new ReadStrings(line1File2, line2File2, line4File2);

				if (!read1.getId().equals(logs[0]) || !read2.getId().equals(logs[1])) {
					throw new Exception("Error in the reads ID");
				}

				gaps1 = logs[6].equals("True");
				gaps1a = logs[7].equals("True");
				gaps2 = logs[8].equals("True");
				gaps2a = logs[9].equals("True");

				junctions1 = Integer.parseInt(logs[10]);
				junctions1a = Integer.parseInt(logs[11]);
				junctions2 = Integer.parseInt(logs[12]);
				junctions2a = Integer.parseInt(logs[13]);

				printMatePairResult(read1, gaps1, gaps1a, junctions1, junctions1a, matePairsFile1Gaps, matePairsFile1Junction, matePairsFile1Match);
				printMatePairResult(read2, gaps2, gaps2a, junctions2, junctions2a, matePairsFile2Gaps, matePairsFile2Junction, matePairsFile2Match);
				// flag is 1 if mismatch is in the end of the valid read otherwise is zero (0)
			}
			matePairsFile1.close();
			matePairsFile2.close();
			matePairsFileLog.close();
			matePairsFile1Gaps.flush();
			matePairsFile1Gaps.close();
			matePairsFile2Gaps.flush();
			matePairsFile2Gaps.close();

			matePairsFile1Junction.flush();
			matePairsFile1Junction.close();
			matePairsFile2Junction.flush();
			matePairsFile2Junction.close();

			matePairsFile1Match.flush();
			matePairsFile1Match.close();
			matePairsFile2Match.flush();
			matePairsFile2Match.close();
		}
		matePairsReads.close();
	}

	private static void printMatePairResult(Read read, boolean gaps, boolean gapsa, int junctions, int junctionsa, BufferedWriter matePairsFileGaps,
			BufferedWriter matePairsFileJunction, BufferedWriter matePairsFileMatch) throws IOException {
		if ((!gaps && junctions == 0) || (!gapsa && junctionsa == 0)) {
			printMatePair(read, matePairsFileMatch);
			case1++;
		} else if ((!gaps && junctions == 1) || (!gapsa && junctionsa == 1)) {
			printMatePair(read, matePairsFileJunction);
			case2++;
		} else {
			// the mate pair doesn't have a good alignment
			printMatePair(read, matePairsFileGaps);
			case3++;
		}
	}

	private static void printMatePair(Read read1, BufferedWriter matePairsFileResult) throws IOException {
		matePairsFileResult.write(read1.getId().replace('@', '>'));
		matePairsFileResult.newLine();
		matePairsFileResult.write(read1.getSequence());
		matePairsFileResult.newLine();
	}

}
